package com.jukte.recipe;

import java.util.ArrayList;
import java.util.List;

import android.annotation.TargetApi;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.ewhat.recipes.R;
import com.jukte.recipe.adapters.RecipeListAdapter;
import com.jukte.recipe.db.DataBaseManager;
import com.jukte.recipe.models.Recipe;

public class RecipeListActivity extends BaseActivity {
	private int catId;
	private int subCatId;
	private String pageTitle;
	private Context con;
	private ListView recipeListView;
	private List<Recipe> recipes;
	private RecipeListAdapter adapter;
	private ProgressDialog m_ProgressDialog;
	private ImageView adView;
	private WebView adWebView;
	private View layoutWebView;
	private String webLink = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_recipe_list);
		
		getIntentData();
		init();
		setUpList();
		fetchDataFromDb();
	}
	
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setLayerTypeToWebview() {
		// TODO Auto-generated method stub
		adWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
	}
	
	
	@Override
	public void init() {
		super.init();
		adView = (ImageView)findViewById(R.id.adView);
		layoutWebView = findViewById(R.id.layoutWebView);
		adWebView = (WebView)findViewById(R.id.adWebView);
		if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            // only for gingerbread and newer versions
			setLayerTypeToWebview();
        }
		adWebView.setBackgroundColor(0x00000000);
		
		tv_title.setText(pageTitle);
		
		
		
		// AD
		if(pageTitle.equals("Breakfast Menu")){
			layoutWebView.setVisibility(View.VISIBLE);
			adWebView.loadUrl("file:///android_res/raw/adweb1.html");
			webLink = "http://tgpcis5.felicitygf.hop.clickbank.net";
		}else if(pageTitle.equals("Soup Menu")){
			adView.setVisibility(View.VISIBLE);
			adView.setBackgroundResource(R.drawable.diabetes2);
			gotoUrl(adView, "http://tgpcis5.DIABETE101.hop.clickbank.net/?cpage=squeeze_2");
		}else if(pageTitle.equals("Salad Menu")){
			adView.setVisibility(View.VISIBLE);
			adView.setBackgroundResource(R.drawable.food4wealth);
			gotoUrl(adView, "http://d4f6fiheeb24fsf8diuscyfw7k.hop.clickbank.net/?tid=APP");
		}else if(pageTitle.equals("Bread/Snacks Menu")){
			layoutWebView.setVisibility(View.VISIBLE);
			adWebView.loadUrl("file:///android_res/raw/adweb2.html");
			webLink = "http://tgpcis5.felicitygf.hop.clickbank.net";
		}else if(pageTitle.equals("Vegetarian Menu")){
			layoutWebView.setVisibility(View.VISIBLE);
			adWebView.loadUrl("file:///android_res/raw/adweb1.html");
			webLink = "http://d4f6fiheeb24fsf8diuscyfw7k.hop.clickbank.net/?tid=APP";
		}else if(pageTitle.equals("Beef/lamb/pork Menu")){
			adView.setVisibility(View.VISIBLE);
			adView.setBackgroundResource(R.drawable.diabetes1);
			gotoUrl(adView, "http://tgpcis5.DIABETE101.hop.clickbank.net");
		}else if(pageTitle.equals("Chicken Menu")){
			adView.setVisibility(View.VISIBLE);
			adView.setBackgroundResource(R.drawable.eatdrinkshrinkplan);
			gotoUrl(adView, "http://e619fipmoey11w6rvap7-xfl44.hop.clickbank.net/?tid=APP");
		}else if(pageTitle.equals("Fish & seafood Menu")){
			adView.setVisibility(View.VISIBLE);
			adView.setBackgroundResource(R.drawable.diabetes3);
			gotoUrl(adView, "http://tgpcis5.DIABETE101.hop.clickbank.net");
		}
		
		
		
		con=RecipeListActivity.this;
		recipeListView=(ListView)findViewById(R.id.recipe_listview);
		recipes=new ArrayList<Recipe>();
	}

	public void onAdWebClick(View v){
		if(webLink.length()==0)return;
		Uri uri = Uri.parse(webLink);
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		startActivity(intent);
	}
	
	private void gotoUrl(View v, final String link){
		v.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Uri uri = Uri.parse(link);
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
			}
		});
	}
	
	private void getIntentData() {
		// TODO Auto-generated method stub
		catId=getIntent().getIntExtra("CATEGORY_ID", 0);
		subCatId=getIntent().getIntExtra("SUB_CAT_ID", 0);
		pageTitle=getIntent().getStringExtra("TITLE");
	}

	private void fetchDataFromDb() {
		Thread thread = new Thread(null, dbFetch, "MagentoBackground");
		thread.start();
		m_ProgressDialog = ProgressDialog.show(this, "",
				"Fetching data...", true);
		m_ProgressDialog.setCancelable(true);
	}

	Runnable dbFetch=new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			getRecipeListFromDb();
			runOnUiThread(returnRecipeData);
		}
	};
	Runnable returnRecipeData=new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			m_ProgressDialog.dismiss();
			adapter.notifyDataSetChanged();
			
		}
	};
	private void setUpList()
	{
				//recipes=new ArrayList<Recipe>();
				adapter=new RecipeListAdapter(RecipeListActivity.this,R.layout.row_recipelist, recipes);
				recipeListView.setAdapter(adapter);
				recipeListView.setOnItemClickListener(new OnItemClickListener() {

					public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
						
						Intent intent=new Intent(con, RecipeDetailsActivity.class);
						intent.putExtra("RECIPE_ID", recipes.get(position).getId());
						intent.putExtra("RECIPE_NAME", recipes.get(position).getName());
						startActivity(intent);
						
					}
				});
	}
	
	private void getRecipeListFromDb() {
		// TODO Auto-generated method stub
		recipes.clear();
		
		if(catId != 0){
			recipes.addAll(DataBaseManager.getInstance().getAllRecipeCatId(catId));
		}else{
			recipes.addAll(DataBaseManager.getInstance().getAllRecipeBySubCatId(subCatId));
		}
		DataBaseManager.getInstance().closeDb();
		//Log.e("aise", sites.size()+"");
	}


}
